package com.ihr360.insurance.model.dto.benefits;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.common.collect.Lists;
import com.ihr360.commons.annotation.CodeGenerator;
import com.ihr360.commons.model.IEnabledEntity;
import com.ihr360.commons.model.base.Division;
import com.ihr360.commons.model.type.BenefitCategory;
import com.ihr360.insurance.model.vo.BenefitModeVo;
import com.ihr360.insurance.model.vo.BenefitTemplateVo;
import com.ihr360.insurance.model.vo.PaymentConfigVo;
import com.ihr360.shared.entitywithlistener.NumberIdWithTime;
import com.ihr360.shared.sequence.handler.CodeGeneratorHandler;
import org.apache.commons.collections.CollectionUtils;
import org.hibernate.annotations.NaturalId;
import org.springframework.beans.BeanUtils;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EntityListeners;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * 社保分组
 */
@Entity
@Table
@EntityListeners(CodeGeneratorHandler.class)
public class MandatoryBenefitGroup extends NumberIdWithTime<Long> implements IEnabledEntity {

    private static final long serialVersionUID = -7480845612897601571L;

    /**
     * 代码
     */
    @CodeGenerator(bizModel = "mandatory_benefit_group", serialTmpl = "SERIALGEN(category,\"_LCT_TYPE_\",MAXID)", name = "社保福利分组序列号", categoryId = "SERIALID(category)")
    @Column(unique = true, nullable = true, length = 50)
    private String code;

    /**
     * 法定福利分类
     */
    @Column(nullable = false, length = 20)
    @NaturalId
    @Enumerated(EnumType.STRING)
    private BenefitCategory category = BenefitCategory.SI;

    /**
     * 行政区划
     */
    @NaturalId(mutable = true)
    @Column(name = "division_id")
    @JsonIgnore
    private Long divisionId;

    /**
     * 缴纳类型
     */
    @ManyToOne(fetch = FetchType.LAZY)
    @NaturalId(mutable = true)
    private MandatoryBenefitsMode mode;

    @Column(length = 1000)
    private String remark;

    /**
     * 法定福利模板
     */
    @OneToMany(fetch = FetchType.LAZY, mappedBy = "group", cascade = CascadeType.ALL, orphanRemoval = true)
    private List<MandatoryBenefitTemplate> templates = new ArrayList<>();

    /**
     * 是否启用
     */
    private Boolean enabled = true;

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    @JsonProperty("division")
    public Division getDivision() {
        return new Division(this.divisionId, "未设置");
    }

    @JsonProperty("division")
    public void setDivision(Division division) {
        if (division == null) {
            return;
        }
        this.divisionId = division.getId();
    }

    public MandatoryBenefitsMode getMode() {
        return mode;
    }

    public void setMode(MandatoryBenefitsMode mode) {
        this.mode = mode;
    }

    @JsonIgnore
    public BenefitModeVo getVoMode() {
        if (mode == null) {
            return null;
        }
        return new BenefitModeVo(mode.getId(), mode.getCode(), mode.getName());
    }

    public String getRemark() {
        return remark;
    }

    public void setRemark(String remark) {
        this.remark = remark;
    }

    @Override
    public boolean isEnabled() {
        return enabled;
    }

    @Override
    public void setEnabled(boolean enabled) {
        this.enabled = enabled;
    }

    public BenefitCategory getCategory() {
        return category;
    }

    public void setCategory(BenefitCategory category) {
        this.category = category;
    }

    public List<MandatoryBenefitTemplate> getTemplates() {
        return templates;
    }

    public void setTemplates(List<MandatoryBenefitTemplate> templates) {
        this.templates = templates;
    }

    @Override
    public String toString() {
        return "MandatoryBenefitGroup{" +
                "code='" + code + '\'' +
                ", divisionId=" + divisionId +
                ", enabled=" + enabled +
                '}';
    }

    /**
     * 外层调用此方法，才能获取到主动获取到templates详情
     *
     * @return
     */
    @JsonIgnore
    public List<BenefitTemplateVo> getInlineTemplates() {
        if (CollectionUtils.isEmpty(templates)) {
            return Collections.emptyList();
        }
        ArrayList<BenefitTemplateVo> results = Lists.newArrayListWithCapacity(templates.size());
        templates.forEach(item -> {
            List<PaymentConfig> configs = item.getConfigs();
            List<PaymentConfigVo> configVos = Lists.newArrayList();
            if (CollectionUtils.isNotEmpty(configs)) {
                configs.forEach(config -> {
                    PaymentConfigVo configVo = new PaymentConfigVo();
                    BeanUtils.copyProperties(config, configVo);
                    configVos.add(configVo);
                });
            }
            results.add(new BenefitTemplateVo(item.getId(), item.getCode(), item.getVoType(), configVos));
        });
        return results;
    }
}
